<?php

/**
 *Simple CRUD Code Generator
 *Email:  support@crazedcoders.com
 *Website: http://www.crazedcoders.com/
 *Copyright (C) 2007 CrazedCoders
 *
 *This program is free software: you can redistribute it and/or modify
 *it under the terms of the GNU General Public License as published by
 *the Free Software Foundation, either version 3 of the License, or
 *(at your option) any later version.
 *
 *THE PROGRAM IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 *IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 *FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 *AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 *LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 *OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 *SOFTWARE.
 *
 *You should have received a copy of the GNU General Public License
 *along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * generates delegate classes for each function in php for every table
 * unlike the other generate* functions this one will write the files rather
 * than returning a string of the files to be written
 */
function generateDelegates( $table ) {

	$className = toCamelCase( $assoc['table_name'] );
	$classNameUp = strtoupper( $className );
	$tableName = $assoc['table_name'];
	$columns = getTableColumns( $tableName );
	$columnsString = join(', ',array_keys($columns));
	$primaryKey = toCamelCaseMember($assoc['column_name']);
	$secondaryKeys = array();
	foreach( $columns as $key=>$value ) {
		$secondaryKeys[$key] = $columns[$key];
	}
	//$secondaryKeys = StdFunc::array_clone( $columns );
	unset( $secondaryKeys[$primaryKey] );

	# create the require_once path
	$requireOnceVoPath = "";


	$export =
'
package '.con::$package.'.business
{
	/**
	 *Simple CRUD Code Generator
	 *Email:  support@crazedcoders.com
	 *Website: http://www.crazedcoders.com/
	 *Copyright (C) 2007 CrazedCoders
	 *
	 *This program is free software: you can redistribute it and/or modify
	 *it under the terms of the GNU General Public License as published by
	 *the Free Software Foundation, either version 3 of the License, or
	 *(at your option) any later version.
	 *
	 *THE PROGRAM IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
	 *IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
	 *FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
	 *AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
	 *LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
	 *OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
	 *SOFTWARE.
	 *
	 *You should have received a copy of the GNU General Public License
	 *along with this program.  If not, see <http://www.gnu.org/licenses/>.
     */

	import com.adobe.cairngorm.control.CairngormEvent;
	import com.adobe.cairngorm.control.CairngormEventDispatcher;
	import com.adobe.cairngorm.commands.ICommand;
	import mx.controls.Alert;
	import com.adobe.cairngorm.business.ServiceLocator;
	import mx.rpc.AsyncToken;
	import mx.collections.ArrayCollection;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.events.FaultEvent;
	import flash.events.Event;
	import mx.rpc.remoting.RemoteObject;
	import mx.utils.ArrayUtil;
	import '.con::$package.'.event.'.$className.'Event;
	import '.con::$package.'.model.'.$className.'Model;
	import com.adobe.cairngorm.model.ModelLocator;
	import '.con::$package.'.event.CommandEvent;
	import mx.rpc.IResponder;
	import '.con::$package.'.vo.'.$className.';


	public class '.$className.'Command implements ICommand, IResponder
	{
		private var _model:'.$className.'Model = '.$className.'Model.getInstance();

		public function '.$className.'Command(){
			super();
		}

		public function execute(event:CairngormEvent):void
		{
			//here you would issue commands based on the eventType
			// we interogate the type because
			// Commands can support multiple events if you so choose
			var service:RemoteObject = ServiceLocator.getInstance()
					.getRemoteObject("'.$className.'DaoRo");
			var token:AsyncToken;
			switch(event.type)
			{
				//defines the event constant in the event object
				case '.$className.'Event.'.$classNameUp.'_ACTION_COUNT_ALL:
					token = service.countAll();
					break;
				//defines the event constant in the event object
				case '.$className.'Event.'.$classNameUp.'_ACTION_GET_ALL:
					token = service.getAll();
					break;
				//defines the event constant in the event object
				case '.$className.'Event.'.$classNameUp.'_ACTION_GET_ALL_BY_START_LENGTH:
					token = service.getAllByStartLength(
						'.$className.'Event(event).paramStart,
						'.$className.'Event(event).paramLength);
					break;
				//defines the event constant in the event object
				case '.$className.'Event.'.$classNameUp.'_ACTION_UPDATE_ONE:
					token = service.updateOne(
						'.$className.'Event(event).paramItem );
					break;
				//defines the event constant in the event object
				case '.$className.'Event.'.$classNameUp.'_ACTION_DELETE_ONE:
					token = service.deleteOne(
						'.$className.'Event(event).paramItem );
					break;
				//defines the event constant in the event object
				case '.$className.'Event.'.$classNameUp.'_ACTION_CREATE_ONE:
					token = service.createOne(
						'.$className.'Event(event).paramItem );
					break;
				//defines the event constant in the event object
				case '.$className.'Event.'.$classNameUp.'_ACTION_GET_ONE:
					token = service.getOne(
						'.$className.'Event(event).paramPrimaryKey );
					break;
				include "'.$className.'_execinclude.as";
			}
			token.addResponder( this );
			//attach original event to token
			token.originator = event;
		}


		public function fault( info:Object ):void {
			// do nothing for now
			Alert.show(FaultEvent(info).fault.message);
		}

		public function result( data:Object ):void {
			var event:ResultEvent = ResultEvent( data );
			var orgEv:'.$className.'Event = event.token.originator;
			switch( orgEv.type ) {
				case '.$className.'Event.'.$classNameUp.'_ACTION_COUNT_ALL:
					this.onCountAll( event );
					break;
				case '.$className.'Event.'.$classNameUp.'_ACTION_GET_ALL:
					this.onGetAll( event );
					break;
				case '.$className.'Event.'.$classNameUp.'_ACTION_GET_ALL_BY_START_LENGTH:
					this.onGetAllByStartLength( event );
					break;
				case '.$className.'Event.'.$classNameUp.'_ACTION_CREATE_ONE:
					this.onCreateOne( event );
					break;
				case '.$className.'Event.'.$classNameUp.'_ACTION_UPDATE_ONE:
					this.onUpdateOne( event );
					break;
				case '.$className.'Event.'.$classNameUp.'_ACTION_DELETE_ONE:
					this.onDeleteOne( event );
					break;
				include "'.$className.'_resultinclude.as";
			}
		}
		include "'.$className.'_include.as";

		// return functions
		private function onCountAll( event:ResultEvent ):void {
			_model.count = int(event.result);
			// distpatch a command event to get picked up by viewcontrollers
			CairngormEventDispatcher.getInstance().dispatchEvent(
				new CommandEvent(
						CommandEvent.SERVICE_RESULT_EVENT,
						event.token.originator,
						null
					));
		}

		private function onGetAll( event:ResultEvent ):void {
			var array:Array = ArrayUtil.toArray( event.result );
			/* for( var i:int = 0; i < array.length; i++ ) {
				_model.add(array[i]);
			} */
			_model.addArray( array );
			// distpatch a command event to get picked up by viewcontrollers
			CairngormEventDispatcher.getInstance().dispatchEvent(
				new CommandEvent(
						CommandEvent.SERVICE_RESULT_EVENT,
						event.token.originator,
						null
					));
		}

		private function onGetAllByStartLength( event:ResultEvent ):void {
			var array:Array = ArrayUtil.toArray( event.result );
			/* for( var i:int = 0; i < array.length; i++ ) {
				_model.add(array[i]);
			} */
			_model.addArray( array );
			// distpatch a command event to get picked up by viewcontrollers
			CairngormEventDispatcher.getInstance().dispatchEvent(
				new CommandEvent(
						CommandEvent.SERVICE_RESULT_EVENT,
						event.token.originator,
						null
					));
		}

		private function onCreateOne( event:ResultEvent ):void {
			//Alert.show( "createOneResult: " + event.result );
			if( event.result ) {
				_model.add( '.$className.'(event.result) );
			}
			else {
				trace( "could not update properly" );
			}
		}
		private function onUpdateOne( event:ResultEvent ):void {
			//Alert.show( "createOneResult: " + event.result );
			if( event.result ) {
				_model.add( '.$className.'Event(event.token.originator).paramItem );
			}
			else {
				trace( "could not update properly" );
			}
		}
		private function onDeleteOne( event:ResultEvent ):void {
			//Alert.show( "createOneResult: " + event.result );
			if( event.result ) {
				_model.remove( '.$className.'Event(event.token.originator).paramItem );
			}
			else {
				trace( "could not delete properly" );
			}
		}

		/* private function onFault(event:FaultEvent):void
		{
			Alert.show(event.fault.message);
		} */
	}
}
';
	return $export;


}

?>
